
\def\SignBoxCornerRadius{.75mm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\BeginTikzPicture{
    \begin{tikzpicture}[x=.4cm,y=.3cm]
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\EndTikzPicture{
    \end{tikzpicture}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print the characters within a string evenly spaced at integral node positions
%
% #1 The number of characters in the string
% #2 The string to print
\newcommand\DrawBitstring[2]{
\foreach \x in {1,...,#1}%
	\draw(\x,0) node{\substring{#2}{\x}{\x}};%
%	\draw(\x,.5) node[text width = 10, text height = 1]{\substring{#2}{\x}{\x}};%	Improve vertical text alignment
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 The total size
% #2 The string to print
% #3 The value to use when extending to left
\newcommand\DrawLeftExtendedBitstring[3]{
	\StrLen{#2}[\numchars]

	\pgfmathsetmacro\leftpadd{int(#1-\numchars)}
	\foreach \x in {1,...,\leftpadd}
    	\draw(\x,0) node{#3};

	\pgfmathsetmacro\leftpadd{int(\leftpadd+1)}
	\foreach \x in {\leftpadd,...,#1}
		\pgfmathsetmacro\ix{int(\x-\leftpadd+1)}
		\draw(\x,0) node{\substring{#2}{\ix}{\ix}};
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% If the string is shorter than expected, extend with #5 to the right.
%
% #1 The total size
% #2 Num chars to extend on the right
% #3 The string to print
% #4 The value to use when extending to left
% #5 The value to use when extending to right
\newcommand\DrawDoubleExtendedBitstring[5]{
	\StrLen{#3}[\numchars]

	\pgfmathsetmacro\leftpadd{int(#1-#2-\numchars)}
	\ifthenelse{1 > \leftpadd}
	{}
	{
		\foreach \x in {1,...,\leftpadd}
    		\draw(\x,0) node{#4};
	}

	\pgfmathsetmacro\leftpadd{int(\leftpadd+1)}
	\pgfmathsetmacro\rightpadd{int(\leftpadd+\numchars)}
	\foreach \x in {\leftpadd,...,\rightpadd}
		\pgfmathsetmacro\ix{int(\x-\leftpadd+1)}
		\draw(\x,0) node{\substring{#3}{\ix}{\ix}};


	%\pgfmathsetmacro\rightpadd{int(\rightpadd+1)}
	\ifthenelse{\rightpadd > #1}	
	{}
	{
		\foreach \x in {\rightpadd,...,#1}
			\draw(\x,0) node{#5};
	}
}




%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw a box suitable to show the given number of bits in a 
% labeled box suitable for showing expanded binary numbers.
%
% #1 The number of characters to display
\newcommand\DrawBitBox[1]{
    \draw (.5,-.75) -- (#1+.5,-.75);		% box bottom
    \draw (.5,.75) -- (#1+.5,.75);			% box top
    \draw (.5,-.75) -- (.5, 1.5);			% left end
    \draw (#1+.5,-.75) -- (#1+.5, 1.5);		% right end
    \pgfmathsetmacro\result{int(#1-1)}		% calc high bit 
    \node at (1,1.2) {\tiny\result};		% high bit label
    \draw(#1,1.2) node{\tiny0};				% low bit label

    \pgfmathsetmacro\result{#1/2}
    \node at (\result,-1.2) {\tiny#1};		% size below the box

    \pgfmathsetmacro\result{#1/2}
    \draw[->] (\result+.6,-1.2) -- (#1+.5,-1.2);
    \draw[->] (\result-.6,-1.2) -- (.5,-1.2);
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxUnsigned[1]{
	\StrLen{#1}[\numchars]
	\DrawBitBox{\numchars}
	\DrawBitstring{\numchars}{#1}		% show the bits
}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxUnsignedPicture[1]{
	\BeginTikzPicture
	\DrawBitBoxUnsigned{#1}
	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxSignedPicture[1]{
    \BeginTikzPicture
    \DrawBitBoxUnsigned{#1}
	% draw a box around the sign bit
	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};
    \EndTikzPicture
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 The total (extended) size
% #2 The value to use for left-side padding
% #3 The string to extend
\newcommand\DrawBitBoxLeftExtended[3]{
	\StrLen{#3}[\numchars]
	\pgfmathsetmacro\fill{int(#1-\numchars)}
	\begin{scope}[shift={(\fill,3.5)}]
    \DrawBitBoxUnsigned{#3}

   	% XXX IFF not zero-extending then draw a box around the sign bit
   	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};
	\end{scope}

	\DrawBitBox{#1}
	\DrawDoubleExtendedBitstring{#1}{0}{#3}{#2}{x}
	
   	% XXX IFF not zero-extending then draw a box around the sign bit
   	\draw {[rounded corners=\SignBoxCornerRadius] (\fill+1.35, -.6) -- (\fill+1.35, .6) -- (\fill+.65, .6) -- (\fill+.65, -.6) -- cycle};
    % draw a box around the extended sign bits
    \draw (.65, -.6) -- (.65, .6) -- (\fill+.35, .6) -- (\fill+.35, -.6) -- cycle;


}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxSignExtendedPicture[2]{
	\BeginTikzPicture
	\DrawBitBoxLeftExtended{#1}{\substring{#2}{1}{1}}{#2}
    \EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawBitBoxZeroExtendedPicture[2]{
	\BeginTikzPicture
	\DrawBitBoxLeftExtended{#1}{0}{#2}
    \EndTikzPicture
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 Total bit length
% #2 The string to print
% #3 Right-side padding length
\newcommand\DrawBitBoxSignLeftZeroRightExtendedPicture[3]{
	\BeginTikzPicture

	\StrLen{#2}[\numchars]
	\pgfmathsetmacro\fill{int(#1-\numchars-#3)}
	\begin{scope}[shift={(\fill,3.5)}]
    \DrawBitBoxUnsigned{#2}
    % draw a box around the sign bit
    %\draw (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle;
    \draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};
	\end{scope}

	\DrawBitBox{#1}
	\DrawDoubleExtendedBitstring{#1}{#3}{#2}{\substring{#2}{1}{1}}{0}

	% Box the sign bit
    \draw {[rounded corners=\SignBoxCornerRadius] (\fill+1.35, -.6) -- (\fill+1.35, .6) -- (\fill+.65, .6) -- (\fill+.65, -.6) -- cycle};

	\ifthenelse{\fill > 0}
	{
    	% Box the left-extended sign bits
    	\draw (.65, -.6) -- (.65, .6) -- (\fill+.35, .6) -- (\fill+.35, -.6) -- cycle;
		% \fill[blue!40!white] (.65, -.6) rectangle (\fill-.25, 1.2);
	}
	{}
	\ifthenelse{#3 > 0}
	{
    	% Box the right-extended sign bits
		\pgfmathsetmacro\posn{int(\numchars+\fill)}
    	\draw (\posn+.65, -.6) -- (\posn+.65, .6) -- (\posn+#3+.35, .6) -- (\posn+#3+.35, -.6) -- cycle;
	}
	{}
	

    \EndTikzPicture
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Draw hex markers
% #1 The number of bits in the box
\newcommand\DrawHexMarkers[1]{
	\pgfmathsetmacro\num{int(#1-1)}
	\foreach \x in {4,8,...,\num}
		\draw [line width=.5mm] (\x+.5,-.75) -- (\x+.5, -.3);
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print the characters within a string evenly spaced at integral node positions
%
% #1 The number of characters in the string
% #2 The string of characters to plot
% #3 Right-side label
\newcommand\DrawInsnBitstring[3]{
	\pgfmathsetmacro\num{int(#1-1)}
	\foreach \x in {1,2,...,#1}
    	\draw(\x+.25,-.3) node[text width = 10, text height = 1]{\substring{#2}{\x}{\x}};
	\draw(#1+1,0) node[right]{#3};
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw a bit-separator line with labels at the given bit-offset (from the right)
%
% #1 Total box width
% #2 The position that the separator will be drawn to the left.
\newcommand\DrawInsnBoxSep[2]{
	\draw (#1-#2-.5,-.75) -- (#1-#2-.5, 1.5);
	\node at (#1-#2,1.2) {\tiny#2};
	\pgfmathsetmacro\result{int(#2+1)}
	\node at (#1-#2-1,1.2) {\tiny\result};
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 total characters/width
% #2 MSB position
% #3 LSB position
% #4 the segment label
\newcommand\DrawInsnBoxSeg[4]{
	\pgfmathsetmacro\leftpos{int(#1-#2)}
	\pgfmathsetmacro\rightpos{int(#1-#3)}

	\draw (\leftpos-.5,-.75) -- (\rightpos+.5,-.75);	% box bottom
	\draw (\leftpos-.5,1.75) -- (\rightpos+.5,1.75);	% box top
	\draw (\leftpos-.5,-.75) -- (\leftpos-.5, 2.5);		% left end
	\draw (\rightpos+.5,-.75) -- (\rightpos+.5, 2.5);	% right end
	\node at (\leftpos,2.2) {\tiny#2};
	\draw(\rightpos,2.2) node{\tiny#3};

	\pgfmathsetmacro\posn{#1-#2+(#2-#3)/2}
	\pgfmathsetmacro\range{int(#2-#3+1)}
	\node at (\posn,1.2) {\small#4};			% the field label

%	\node at (\posn,-1.4) {\small\range};		% the field width
	\begin{scope}[shift={(0,-.7)}]\InsnBoxFieldWidthArrow{#2}{#3}\end{scope}

%	% arrows showing the span of the bits... meh
%    \draw[->] (\posn+.5,-1.4) -- (\rightpos+.2,-1.4);
%    \draw[->] (\posn-.5,-1.4) -- (\leftpos-.2,-1.4);
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\InsnStatement[1]{
%	\textbf{\large #1}\\
%	\textbf{#1}\\
	{\large #1}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawInsnTypeB[1]{
	\StrLen{#1}[\numchars]
	\begin{scope}[shift={(0,.75)}]	
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:btype]{B-type}}
	\DrawInsnBoxSeg{\numchars}{31}{25}{imm[12\textbar10:5]}
	\DrawInsnBoxSeg{\numchars}{24}{20}{rs2}
	\DrawInsnBoxSeg{\numchars}{19}{15}{rs1}
	\DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
	\DrawInsnBoxSeg{\numchars}{11}{7}{imm[4:1\textbar11]}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

	% add some hint bits in for imm fields
	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};	% sign bit
	\draw (32-7-.5, -.75) -- (32-7-.5, .1);		% imm[11]
	\draw (32-30-.5, -.75) -- (32-30.5, .1);	% imm[12]

	\end{scope}

	\DrawHexMarkersRel{\numchars}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeBTikz[1]{
	\BeginTikzPicture
	\DrawInsnTypeB{#1}
	\EndTikzPicture
}

\newcommand\DrawInsnTypeBPicture[2]{
	\InsnStatement{#1}\\
	\DrawInsnTypeBTikz{#2}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeU[1]{
	\StrLen{#1}[\numchars]
	\begin{scope}[shift={(0,.75)}]
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:utype]{U-type}}
	\DrawInsnBoxSeg{\numchars}{31}{12}{imm[31:12]}
	\DrawInsnBoxSeg{\numchars}{11}{7}{rd}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

	% add some hint bits in for imm fields
	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};	% sign bit
	\end{scope}

	\DrawHexMarkersRel{\numchars}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeUTikz[1]{
	\BeginTikzPicture
	\DrawInsnTypeU{#1}
	\EndTikzPicture
}

\newcommand\DrawInsnTypeUPicture[2]{
	\InsnStatement{#1}\\
	\DrawInsnTypeUTikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeJ[1]{
	\StrLen{#1}[\numchars]
	\begin{scope}[shift={(0,.75)}]	
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:jtype]{J-type}}

	\DrawInsnBoxSeg{\numchars}{31}{12}{imm[20\textbar10:1\textbar11\textbar19:12]}
	\DrawInsnBoxSeg{\numchars}{11}{7}{rd}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}
%	\DrawHexMarkers{\numchars}
	\end{scope}

	% add some hint bits in for imm fields
	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, .15) -- (1.35, 1.35) -- (.65, 1.35) -- (.65, .15) -- cycle};	% sign bit
	\draw (32-19-.5, 0) -- (32-19.5, .85);		% imm[19:12]
	\draw (32-20-.5, 0) -- (32-20.5, .85);		% imm[11]
	\draw (32-30-.5, 0) -- (32-30.5, .85);		% imm[1:10]
	\DrawHexMarkersRel{\numchars}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeJTikz[1]{
	\BeginTikzPicture
	\DrawInsnTypeJ{#1}
%	\StrLen{#1}[\numchars]
%	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:jtype]{J-type}}
%	\DrawInsnBoxSeg{\numchars}{31}{12}{imm[20\textbar10:1\textbar11\textbar19:12]}
%	\DrawInsnBoxSeg{\numchars}{11}{7}{rd}
%	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}
%
%	% add some hint bits in for imm fields
%	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};	% sign bit
%	\draw (32-19-.5, -.75) -- (32-19.5, .1);		% imm[19:12]
%	\draw (32-20-.5, -.75) -- (32-20.5, .1);		% imm[11]
%	\DrawHexMarkers{\numchars}

	\EndTikzPicture
}

\newcommand\DrawInsnTypeJPicture[2]{
	\InsnStatement{#1}\\
	\DrawInsnTypeJTikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeI[1]{
	\StrLen{#1}[\numchars]
	\begin{scope}[shift={(0,.75)}]	
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:itype]{I-type}}
	\DrawInsnBoxSeg{\numchars}{31}{20}{imm[11:0]}
	\DrawInsnBoxSeg{\numchars}{19}{15}{rs1}
	\DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
	\DrawInsnBoxSeg{\numchars}{11}{7}{rd}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

	% add some hint bits in for imm fields
	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};	% sign bit
	\end{scope}
	\DrawHexMarkersRel{\numchars}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeITikz[1]{
	\BeginTikzPicture
	\DrawInsnTypeI{#1}
%	\DrawHexMarkers{\numchars}
	\EndTikzPicture
}
\newcommand\DrawInsnTypeIPicture[2]{
	\InsnStatement{#1}\\
	\DrawInsnTypeITikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeS[1]{
	\StrLen{#1}[\numchars]
	\begin{scope}[shift={(0,.75)}]
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:stype]{S-type}}
	\DrawInsnBoxSeg{\numchars}{31}{25}{imm[11:5]}
	\DrawInsnBoxSeg{\numchars}{24}{20}{rs2}
	\DrawInsnBoxSeg{\numchars}{19}{15}{rs1}
	\DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
	\DrawInsnBoxSeg{\numchars}{11}{7}{imm[4:0]}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

	% add some hint bits in for imm fields
	\draw {[rounded corners=\SignBoxCornerRadius] (1.35, -.6) -- (1.35, .6) -- (.65, .6) -- (.65, -.6) -- cycle};	% sign bit
	\end{scope}

	\DrawHexMarkersRel{\numchars}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeSTikz[1]{
	\BeginTikzPicture
	\DrawInsnTypeS{#1}
	\EndTikzPicture
}

\newcommand\DrawInsnTypeSPicture[2]{
	\InsnStatement{#1}\\
	\DrawInsnTypeSTikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeIShiftTikz[1]{
	\BeginTikzPicture
	\StrLen{#1}[\numchars]
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:rtype]{I-type}}
	\DrawInsnBoxSeg{\numchars}{31}{25}{funct7}
	\DrawInsnBoxSeg{\numchars}{24}{20}{shamt}
	\DrawInsnBoxSeg{\numchars}{19}{15}{rs1}
	\DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
	\DrawInsnBoxSeg{\numchars}{11}{7}{rd}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

	\DrawHexMarkers{\numchars}
	\EndTikzPicture
}

\newcommand\DrawInsnTypeRShiftPicture[2]{
	\InsnStatement{#1}\\
	\DrawInsnTypeIShiftTikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeRTikz[1]{
	\BeginTikzPicture
	\StrLen{#1}[\numchars]
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:rtype]{R-type}}
	\DrawInsnBoxSeg{\numchars}{31}{25}{funct7}
	\DrawInsnBoxSeg{\numchars}{24}{20}{rs2}
	\DrawInsnBoxSeg{\numchars}{19}{15}{rs1}
	\DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
	\DrawInsnBoxSeg{\numchars}{11}{7}{rd}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

	\DrawHexMarkers{\numchars}
	\EndTikzPicture
}

\newcommand\DrawInsnTypeRPicture[2]{
	\InsnStatement{#1}\\
	\DrawInsnTypeRTikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeFTikz[1]{
    \BeginTikzPicture
    \StrLen{#1}[\numchars]
    \DrawInsnBitstring{\numchars}{#1}{FENCE}
    \DrawInsnBoxSeg{\numchars}{31}{28}{}
    \DrawInsnBoxSeg{\numchars}{27}{24}{pred}
    \DrawInsnBoxSeg{\numchars}{23}{20}{succ}
    \DrawInsnBoxSeg{\numchars}{19}{15}{}
    \DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
    \DrawInsnBoxSeg{\numchars}{11}{7}{}
    \DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

    \DrawHexMarkers{\numchars}
    \EndTikzPicture
}

\newcommand\DrawInsnTypeFPicture[2]{
    \InsnStatement{#1}\\
    \DrawInsnTypeFTikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeETikz[1]{
    \BeginTikzPicture
	\StrLen{#1}[\numchars]
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:itype]{I-type}}
	\DrawInsnBoxSeg{\numchars}{31}{20}{}
	\DrawInsnBoxSeg{\numchars}{19}{15}{}
	\DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
	\DrawInsnBoxSeg{\numchars}{11}{7}{}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

    \DrawHexMarkers{\numchars}
    \EndTikzPicture
}

\newcommand\DrawInsnTypeEPicture[2]{
    \InsnStatement{#1}\\
    \DrawInsnTypeETikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeCSTikz[1]{
    \BeginTikzPicture
	\StrLen{#1}[\numchars]
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:itype]{I-type}}
	\DrawInsnBoxSeg{\numchars}{31}{20}{csr}
	\DrawInsnBoxSeg{\numchars}{19}{15}{rs1}
	\DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
	\DrawInsnBoxSeg{\numchars}{11}{7}{rd}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

    \DrawHexMarkers{\numchars}
    \EndTikzPicture
}

\newcommand\DrawInsnTypeCSPicture[2]{
    \InsnStatement{#1}\\
    \DrawInsnTypeCSTikz{#2}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 the binary encoding
\newcommand\DrawInsnTypeCSITikz[1]{
    \BeginTikzPicture
	\StrLen{#1}[\numchars]
	\DrawInsnBitstring{\numchars}{#1}{\hyperref[insnformat:itype]{I-type}}
	\DrawInsnBoxSeg{\numchars}{31}{20}{csr}
	\DrawInsnBoxSeg{\numchars}{19}{15}{zimm}
	\DrawInsnBoxSeg{\numchars}{14}{12}{funct3}
	\DrawInsnBoxSeg{\numchars}{11}{7}{rd}
	\DrawInsnBoxSeg{\numchars}{6}{0}{opcode}

    \DrawHexMarkers{\numchars}
    \EndTikzPicture
}

\newcommand\DrawInsnTypeCSIPicture[2]{
    \InsnStatement{#1}\\
    \DrawInsnTypeCSITikz{#2}
}



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


\newcommand\xTInsnStatement[4]{%
	\parbox{3.5cm}{{\sffamily\large\bfseries #2}\\
	\tt#3}\hspace{5mm}\parbox{5cm}{\bfseries#1}\parbox{12cm}{#4}%
}

\newcommand\TInsnStatement[4]{%
	\begin{tabular}{lll}
	\parbox[t]{3.5cm}{{\sffamily\large\bfseries #2}\\
	\tt#3} & \parbox[t]{5cm}{\bfseries #1} & \parbox[t]{12cm}{#4}\\
	\end{tabular}
}

\newcommand\TDrawInsnTypeUPicture[5]{%
	\TInsnStatement{#1}{#2}{#3}{#4}\\
    \DrawInsnTypeUTikz{#5}%
}

\newcommand\TDrawInsnTypeJPicture[5]{%
	\TInsnStatement{#1}{#2}{#3}{#4}\\
    \DrawInsnTypeJTikz{#5}%
}

\newcommand\TDrawInsnTypeBPicture[5]{%
	\TInsnStatement{#1}{#2}{#3}{#4}\\
    \DrawInsnTypeBTikz{#5}%
}

\newcommand\TDrawInsnTypeIPicture[5]{%
	\TInsnStatement{#1}{#2}{#3}{#4}\\
    \DrawInsnTypeITikz{#5}%
}
\newcommand\TDrawInsnTypeSPicture[5]{%
	\TInsnStatement{#1}{#2}{#3}{#4}\\
    \DrawInsnTypeSTikz{#5}%
}
\newcommand\TDrawInsnTypeRPicture[5]{%
	\TInsnStatement{#1}{#2}{#3}{#4}\\
    \DrawInsnTypeRTikz{#5}%
}

\newcommand\TDrawInsnTypeRShiftPicture[5]{
	\TInsnStatement{#1}{#2}{#3}{#4}\\
	\DrawInsnTypeIShiftTikz{#5}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw hex markers with a baseline at zero
% #1 The number of bits in the box
\newcommand\DrawHexMarkersRel[1]{
	\pgfmathsetmacro\num{int(#1-1)}
	\foreach \x in {4,8,...,\num}
		\draw [line width=.5mm] (\x+.5,0) -- (\x+.5, .4);
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw an instruction box with a baseline at zero
% #1 MSB position
% #2 LSB position
% #3 the segment label
\newcommand\DrawInsnBoxRelTop[3]{
	\pgfmathsetmacro\leftpos{int(32-#1)}
	\pgfmathsetmacro\rightpos{int(32-#2)}
	\draw (\leftpos-.5,1.5) -- (\rightpos+.5,1.5);	% box top
	\draw (\leftpos-.5,0) -- (\leftpos-.5, 1.5);	% left end
	\draw (\rightpos+.5,0) -- (\rightpos+.5, 1.5);	% right end
	\pgfmathsetmacro\posn{32-#1+(#1-#2)/2}
	\node at (\posn,.75) {\small#3};				% the field label
}

% Draw only the bottom line of an instruction box with a baseline at zero
% #1 MSB position
% #2 LSB position
\newcommand\DrawInsnBoxRelBottom[2]{
	\pgfmathsetmacro\leftpos{int(32-#1)}
	\pgfmathsetmacro\rightpos{int(32-#2)}
	\draw (\leftpos-.5,0) -- (\rightpos+.5,0);		% box bottom
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Draw an instruction box with a baseline at zero
% #1 MSB position
% #2 LSB position
% #3 the segment label
\newcommand\DrawInsnBoxRel[3]{
	\DrawInsnBoxRelTop{#1}{#2}{#3}
	\DrawInsnBoxRelBottom{#1}{#2}
}

%\newcommand\DrawInsnBoxRel[3]{
%	\pgfmathsetmacro\leftpos{int(32-#1)}
%	\pgfmathsetmacro\rightpos{int(32-#2)}
%
%	\draw (\leftpos-.5,0) -- (\rightpos+.5,0);	% box bottom
%	\draw (\leftpos-.5,1.5) -- (\rightpos+.5,1.5);	% box top
%	\draw (\leftpos-.5,0) -- (\leftpos-.5, 1.5);		% left end
%	\draw (\rightpos+.5,0) -- (\rightpos+.5, 1.5);	% right end
%
%    \pgfmathsetmacro\posn{32-#1+(#1-#2)/2}
%    \node at (\posn,.75) {\small#3};            % the field label
%}

% #1 MSB position
% #2 LSB position
\newcommand\DrawInsnBoxCastle[2]{
	\pgfmathsetmacro\leftpos{int(32-#1)}
	\pgfmathsetmacro\rightpos{int(32-#2)}
	\draw (\leftpos-.5,0) -- (\leftpos-.5, .75);		% left end
	\draw (\rightpos+.5,0) -- (\rightpos+.5, .75);	% right end
	\node at (\leftpos,.5) {\tiny#1};
	\ifthenelse{\equal{#1}{#2}}
	{}
	{ \draw(\rightpos,.5) node{\tiny#2}; }
}
\newcommand\DrawInsnBoxCastleRtype{
	\DrawInsnBoxCastle{31}{25}
	\DrawInsnBoxCastle{24}{20}
	\DrawInsnBoxCastle{19}{15}
	\DrawInsnBoxCastle{14}{12}
	\DrawInsnBoxCastle{11}{7}
	\DrawInsnBoxCastle{6}{0}
}

\newcommand\DrawInsnBoxCastleJtype{
	\DrawInsnBoxCastle{31}{31}
	\DrawInsnBoxCastle{30}{21}
	\DrawInsnBoxCastle{20}{20}
	\DrawInsnBoxCastle{19}{12}
	\DrawInsnBoxCastle{11}{7}
	\DrawInsnBoxCastle{6}{0}
}


\newcommand\DrawInsnBoxLabelsBtype{
	\DrawInsnBoxRel{31}{25}{imm[12\textbar10:5]}
	\DrawInsnBoxRel{24}{20}{rs2}
	\DrawInsnBoxRel{19}{15}{rs1}
	\DrawInsnBoxRel{14}{12}{funct3}
	\DrawInsnBoxRel{11}{7}{imm[4:1\textbar11]}
	\DrawInsnBoxRel{6}{0}{opcode}
	\draw(33,.75) node[right]{\hyperref[insnformat:btype]{B-type}};
}
\newcommand\DrawInsnBoxLabelsRtype{
	\DrawInsnBoxRel{31}{25}{funct7}
	\DrawInsnBoxRel{24}{20}{rs2}
	\DrawInsnBoxRel{19}{15}{rs1}
	\DrawInsnBoxRel{14}{12}{funct3}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{opcode}
	\draw(33,.75) node[right]{\hyperref[insnformat:rtype]{R-type}};
}
\newcommand\DrawInsnBoxLabelsItype{
	\DrawInsnBoxRel{31}{20}{imm[11:0]}
	\DrawInsnBoxRel{19}{15}{rs1}
	\DrawInsnBoxRel{14}{12}{funct3}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{opcode}
	\draw(33,.75) node[right]{\hyperref[insnformat:itype]{I-type}};
}
\newcommand\DrawInsnBoxLabelsStype{
    \DrawInsnBoxRel{31}{25}{imm[11:5]}
    \DrawInsnBoxRel{24}{20}{rs2}
    \DrawInsnBoxRel{19}{15}{rs1}
    \DrawInsnBoxRel{14}{12}{funct3}
    \DrawInsnBoxRel{11}{7}{imm[4:0]}
    \DrawInsnBoxRel{6}{0}{opcode}
	\draw(33,.75) node[right]{\hyperref[insnformat:stype]{S-type}};
}
\newcommand\DrawInsnBoxLabelsUtype{
	\DrawInsnBoxRel{31}{12}{imm[31:12]}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{opcode}
	\draw(33,.75) node[right]{\hyperref[insnformat:utype]{U-type}};
}
\newcommand\DrawInsnBoxLabelsJtype{
	\DrawInsnBoxRel{31}{12}{imm[20\textbar10:1\textbar11\textbar19:12]}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{opcode}
	\draw(33,.75) node[right]{\hyperref[insnformat:jtype]{J-type}};
}

\newcommand\DrawAllInsnTypes{
	\BeginTikzPicture
	\DrawInsnBoxLabelsRtype
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnBoxLabelsItype\end{scope}
	\begin{scope}[shift={(0,-3)}]\DrawInsnBoxLabelsStype\end{scope}
	\begin{scope}[shift={(0,-4.5)}]\DrawInsnBoxLabelsBtype\end{scope}
	\begin{scope}[shift={(0,-6)}]\DrawInsnBoxLabelsUtype\end{scope}
	\begin{scope}[shift={(0,-7.5)}]\DrawInsnBoxLabelsJtype\DrawHexMarkersRel{32}\end{scope}

	\begin{scope}[shift={(0,1.5)}]
		\DrawInsnBoxCastleRtype
	\end{scope}

	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Print the characters within a string evenly spaced at integral node positions
% #1 The string of characters to plot
\newcommand\DrawBitstringX[1]{
	\StrLen{#1}[\numchars]
	\pgfmathsetmacro\num{int(\numchars-1)}
	\foreach \x in {1,2,...,\numchars}
%    	\draw(\x,0) node{\substring{\strut #1}{\x}{\x}};
		\draw(\x+.25,.5) node[text width = 10, text height = 1]{\substring{#1}{\x}{\x}};
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\InsnBoxTypePosY{32.5}
\newcommand\InsnBoxMnemonicPosY{36.5}
\newcommand\InsnBoxMnemonicArgPosY{39.5}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 func3
% #3 mnemonic
% #4 args
\newcommand\DrawInsnOpBType[4]{
	\DrawInsnBoxRel{31}{25}{imm[12\textbar10:5]}
	\DrawInsnBoxRel{24}{20}{rs2}
	\DrawInsnBoxRel{19}{15}{rs1}
	\DrawInsnBoxRel{14}{12}{}
	\DrawInsnBoxRel{11}{7}{imm[4:1\textbar11]}
	\DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:btype]{B-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #3};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #4};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
	\begin{scope}[shift={(31-14,0)}]\DrawBitstringX{#2}\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 func3
% #3 mnemonic
% #4 args
\newcommand\DrawInsnOpIType[4]{
	\DrawInsnBoxRel{31}{20}{imm[11:0]}
	\DrawInsnBoxRel{19}{15}{rs1}
	\DrawInsnBoxRel{14}{12}{}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:itype]{I-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #3};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #4};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
	\begin{scope}[shift={(31-14,0)}]\DrawBitstringX{#2}\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 func3
% #3 mnemonic
% #4 args
% #5 rs1/zimm
\newcommand\DrawInsnOpITypeSystem[5]{
	\DrawInsnBoxRel{31}{20}{csr[11:0]}
	\DrawInsnBoxRel{19}{15}{#5}
	\DrawInsnBoxRel{14}{12}{}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:itype]{I-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #3};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #4};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
	\begin{scope}[shift={(31-14,0)}]\DrawBitstringX{#2}\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 func3
% #3 func7
% #4 mnemonic
% #5 args
\newcommand\DrawInsnOpITypeShift[5]{
    \DrawInsnBoxRel{31}{25}{}
    \DrawInsnBoxRel{24}{20}{shamt}
    \DrawInsnBoxRel{19}{15}{rs1}
    \DrawInsnBoxRel{14}{12}{}
    \DrawInsnBoxRel{11}{7}{rd}
    \DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:itype]{I-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #4};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #5};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
	\begin{scope}[shift={(31-14,0)}]\DrawBitstringX{#2}\end{scope}
	\begin{scope}[shift={(31-31,0)}]\DrawBitstringX{#3}\end{scope}
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 func3
% #3 mnemonic
% #4 args
\newcommand\DrawInsnOpSType[4]{
    \DrawInsnBoxRel{31}{25}{imm[11:5]}
    \DrawInsnBoxRel{24}{20}{rs2}
    \DrawInsnBoxRel{19}{15}{rs1}
    \DrawInsnBoxRel{14}{12}{}
    \DrawInsnBoxRel{11}{7}{imm[4:0]}
    \DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:stype]{S-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #3};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #4};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
	\begin{scope}[shift={(31-14,0)}]\DrawBitstringX{#2}\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 func3
% #3 func7
% #4 mnemonic
% #5 args
\newcommand\DrawInsnOpRType[5]{
	\DrawInsnBoxRel{31}{25}{}
	\DrawInsnBoxRel{24}{20}{rs2}
	\DrawInsnBoxRel{19}{15}{rs1}
	\DrawInsnBoxRel{14}{12}{}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:rtype]{R-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #4};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #5};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
	\begin{scope}[shift={(31-14,0)}]\DrawBitstringX{#2}\end{scope}
	\begin{scope}[shift={(31-31,0)}]\DrawBitstringX{#3}\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 func3
% #3 mnemonic
% #4 args
\newcommand\DrawInsnOpFenceType[4]{
	\DrawInsnBoxRel{31}{28}{}
	\DrawInsnBoxRel{27}{24}{pred}
	\DrawInsnBoxRel{23}{20}{succ}
	\DrawInsnBoxRel{19}{15}{}
	\DrawInsnBoxRel{14}{12}{}
	\DrawInsnBoxRel{11}{7}{}
	\DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #3};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #4};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
	\begin{scope}[shift={(31-14,0)}]\DrawBitstringX{#2}\end{scope}
	\begin{scope}[shift={(31-31,0)}]\DrawBitstringX{0000}\end{scope}

	\begin{scope}[shift={(31-19,0)}]\DrawBitstringX{00000}\end{scope}
	\begin{scope}[shift={(31-11,0)}]\DrawBitstringX{00000}\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 func12
% #3 mnemonic
% #4 args
\newcommand\DrawInsnOpSysType[3]{
	\DrawInsnBoxRel{31}{20}{}
	\DrawInsnBoxRel{19}{15}{}
	\DrawInsnBoxRel{14}{12}{}
	\DrawInsnBoxRel{11}{7}{}
	\DrawInsnBoxRel{6}{0}{}
%	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:rtype]{R-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #3};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
	\begin{scope}[shift={(31-11,0)}]\DrawBitstringX{00000}\end{scope}
	\begin{scope}[shift={(31-14,0)}]\DrawBitstringX{000}\end{scope}
	\begin{scope}[shift={(31-19,0)}]\DrawBitstringX{00000}\end{scope}
	\begin{scope}[shift={(31-31,0)}]\DrawBitstringX{#2}\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 mnemonic
% #4 args
\newcommand\DrawInsnOpUType[3]{
	\DrawInsnBoxRel{31}{12}{imm[31:12]}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:utype]{U-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #2};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #3};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% #1 opcode
% #2 mnemonic
% #4 args
\newcommand\DrawInsnOpJType[3]{
	\DrawInsnBoxRel{31}{12}{imm[20\textbar10:1\textbar11\textbar19:12]}
	\DrawInsnBoxRel{11}{7}{rd}
	\DrawInsnBoxRel{6}{0}{}
	\draw(\InsnBoxTypePosY,.75) node[right]{\hyperref[insnformat:jtype]{J-type}};
	\draw(\InsnBoxMnemonicPosY,.75) node[right]{\tt #2};
	\draw(\InsnBoxMnemonicArgPosY,.75) node[right]{\tt #3};

	\begin{scope}[shift={(31-6,0)}]\DrawBitstringX{#1}\end{scope}

}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\DrawAllInsnOpsU{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpUType{0110111}{\hyperref[insn:lui]{lui}}{rd,\hyperref[imm.u:decode]{imm}}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpUType{0010111}{\hyperref[insn:auipc]{auipc}}{rd,\hyperref[imm.u:decode]{imm}}\end{scope}
%	\begin{scope}[shift={(0,-1.5)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsJAL{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpJType{1101111}{\hyperref[insn:jal]{jal}}{rd,\hyperref[pcrel.21]{pcrel\_21}}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpIType{1100111}{000}{\hyperref[insn:jalr]{jalr}}{rd,\hyperref[imm.i:decode]{imm}(rs1)}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsBranch{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpBType{1100011}{000}{\hyperref[insn:beq]{beq}}{rs1,rs2,\hyperref[pcrel.13]{pcrel\_13}}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpBType{1100011}{001}{\hyperref[insn:bne]{bne}}{rs1,rs2,\hyperref[pcrel.13]{pcrel\_13}}\end{scope}
	\begin{scope}[shift={(0,-3)}]\DrawInsnOpBType{1100011}{100}{\hyperref[insn:blt]{blt}}{rs1,rs2,\hyperref[pcrel.13]{pcrel\_13}}\end{scope}
	\begin{scope}[shift={(0,-4.5)}]\DrawInsnOpBType{1100011}{101}{\hyperref[insn:bge]{bge}}{rs1,rs2,\hyperref[pcrel.13]{pcrel\_13}}\end{scope}
	\begin{scope}[shift={(0,-6)}]\DrawInsnOpBType{1100011}{110}{\hyperref[insn:bltu]{bltu}}{rs1,rs2,\hyperref[pcrel.13]{pcrel\_13}}\end{scope}
	\begin{scope}[shift={(0,-7.5)}]\DrawInsnOpBType{1100011}{111}{\hyperref[insn:bgeu]{bgeu}}{rs1,rs2,\hyperref[pcrel.13]{pcrel\_13}}\end{scope}

	\begin{scope}[shift={(0,-7.5)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsLoad{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpIType{0000011}{000}{\hyperref[insn:lb]{lb}}{rd,\hyperref[imm.i:decode]{imm}(rs1)}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpIType{0000011}{001}{\hyperref[insn:lh]{lh}}{rd,\hyperref[imm.i:decode]{imm}(rs1)}\end{scope}
	\begin{scope}[shift={(0,-3.0)}]\DrawInsnOpIType{0000011}{010}{\hyperref[insn:lw]{lw}}{rd,\hyperref[imm.i:decode]{imm}(rs1)}\end{scope}
	\begin{scope}[shift={(0,-4.5)}]\DrawInsnOpIType{0000011}{100}{\hyperref[insn:lbu]{lbu}}{rd,\hyperref[imm.i:decode]{imm}(rs1)}\end{scope}
	\begin{scope}[shift={(0,-6.0)}]\DrawInsnOpIType{0000011}{101}{\hyperref[insn:lhu]{lhu}}{rd,\hyperref[imm.i:decode]{imm}(rs1)}\end{scope}

	\begin{scope}[shift={(0,-6.0)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsStore{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpSType{0100011}{000}{\hyperref[insn:sb]{sb}}{rs2,\hyperref[imm.s:decode]{imm}(rs1)}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpSType{0100011}{001}{\hyperref[insn:sh]{sh}}{rs2,\hyperref[imm.s:decode]{imm}(rs1)}\end{scope}
	\begin{scope}[shift={(0,-3.0)}]\DrawInsnOpSType{0100011}{010}{\hyperref[insn:sw]{sw}}{rs2,\hyperref[imm.s:decode]{imm}(rs1)}\end{scope}

	\begin{scope}[shift={(0,-3.0)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsALUImm{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpIType{0010011}{000}{\hyperref[insn:addi]{addi}}{rd,rs1,\hyperref[imm.i:decode]{imm}}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpIType{0010011}{010}{\hyperref[insn:slti]{slti}}{rd,rs1,\hyperref[imm.i:decode]{imm}}\end{scope}
	\begin{scope}[shift={(0,-3.0)}]\DrawInsnOpIType{0010011}{011}{\hyperref[insn:sltiu]{sltiu}}{rd,rs1,\hyperref[imm.i:decode]{imm}}\end{scope}
	\begin{scope}[shift={(0,-4.5)}]\DrawInsnOpIType{0010011}{100}{\hyperref[insn:xori]{xori}}{rd,rs1,\hyperref[imm.i:decode]{imm}}\end{scope}
	\begin{scope}[shift={(0,-6.0)}]\DrawInsnOpIType{0010011}{110}{\hyperref[insn:ori]{ori}}{rd,rs1,\hyperref[imm.i:decode]{imm}}\end{scope}
	\begin{scope}[shift={(0,-7.5)}]\DrawInsnOpIType{0010011}{111}{\hyperref[insn:andi]{andi}}{rd,rs1,\hyperref[imm.i:decode]{imm}}\end{scope}

	\begin{scope}[shift={(0,-7.5)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsShiftImm{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpITypeShift{0010011}{001}{0000000}{\hyperref[insn:slli]{slli}}{rd,rs1,\hyperref[shamt.i:decode]{shamt}}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpITypeShift{0010011}{101}{0000000}{\hyperref[insn:srli]{srli}}{rd,rs1,\hyperref[shamt.i:decode]{shamt}}\end{scope}
	\begin{scope}[shift={(0,-3.0)}]\DrawInsnOpITypeShift{0010011}{101}{0100000}{\hyperref[insn:srai]{srai}}{rd,rs1,\hyperref[shamt.i:decode]{shamt}}\end{scope}

	\begin{scope}[shift={(0,-3.0)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsALUR{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpRType{0110011}{000}{0000000}{\hyperref[insn:add]{add}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpRType{0110011}{000}{0100000}{\hyperref[insn:sub]{sub}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-3.0)}]\DrawInsnOpRType{0110011}{001}{0000000}{\hyperref[insn:sll]{sll}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-4.5)}]\DrawInsnOpRType{0110011}{010}{0000000}{\hyperref[insn:slt]{slt}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-6.0)}]\DrawInsnOpRType{0110011}{011}{0000000}{\hyperref[insn:sltu]{sltu}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-7.5)}]\DrawInsnOpRType{0110011}{100}{0000000}{\hyperref[insn:xor]{xor}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-9.0)}]\DrawInsnOpRType{0110011}{101}{0000000}{\hyperref[insn:srl]{srl}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-10.5)}]\DrawInsnOpRType{0110011}{101}{0100000}{\hyperref[insn:sra]{sra}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-12.0)}]\DrawInsnOpRType{0110011}{110}{0000000}{\hyperref[insn:or]{or}}{rd,rs1,rs2}\end{scope}
	\begin{scope}[shift={(0,-13.5)}]\DrawInsnOpRType{0110011}{111}{0000000}{\hyperref[insn:and]{and}}{rd,rs1,rs2}\end{scope}

	\begin{scope}[shift={(0,-13.5)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsFence{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpFenceType{0001111}{000}{\hyperref[insn:fence]{fence}}{pred,succ}\end{scope}
	%\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}
}
\newcommand\DrawAllInsnOpsSim{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpSysType{1110011}{000000000000}{\hyperref[insn:ecall]{ecall}}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpSysType{1110011}{000000000001}{\hyperref[insn:ebreak]{ebreak}}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawHexMarkersRel{32}\end{scope}
}

\newcommand\DrawAllInsnOpsSystem{
	\begin{scope}[shift={(0,0)}]\DrawInsnOpITypeSystem{1110011}{001}{\hyperref[insn:csrrw]{csrrw}}{rd,\hyperref[csr.i:decode]{csr},rs1}{rs1}\end{scope}
	\begin{scope}[shift={(0,-1.5)}]\DrawInsnOpITypeSystem{1110011}{010}{\hyperref[insn:csrrs]{csrrs}}{rd,\hyperref[csr.i:decode]{csr},rs1}{rs1}\end{scope}
	\begin{scope}[shift={(0,-3.0)}]\DrawInsnOpITypeSystem{1110011}{011}{\hyperref[insn:csrrc]{csrrc}}{rd,\hyperref[csr.i:decode]{csr},rs1}{rs1}\end{scope}
	\begin{scope}[shift={(0,-4.5)}]\DrawInsnOpITypeSystem{1110011}{101}{\hyperref[insn:csrrwi]{csrrwi}}{rd,\hyperref[csr.i:decode]{csr},zimm}{zimm[4:0]}\end{scope}
	\begin{scope}[shift={(0,-6.0)}]\DrawInsnOpITypeSystem{1110011}{110}{\hyperref[insn:csrrsi]{csrrsi}}{rd,\hyperref[csr.i:decode]{csr},zimm}{zimm[4:0]}\end{scope}
	\begin{scope}[shift={(0,-7.5)}]\DrawInsnOpITypeSystem{1110011}{111}{\hyperref[insn:csrrci]{csrrci}}{rd,\hyperref[csr.i:decode]{csr},zimm}{zimm[4:0]}\end{scope}
	\begin{scope}[shift={(0,-7.5)}]\DrawHexMarkersRel{32}\end{scope}
}

\newcommand\DrawAllInsnOps{
	\BeginTikzPicture
	\begin{scope}[shift={(0,1.5)}]\DrawInsnBoxCastleRtype\end{scope}
	\begin{scope}[shift={(0,0)}]\DrawAllInsnOpsU\end{scope}
	\begin{scope}[shift={(0,-3.0)}]\DrawAllInsnOpsJAL\end{scope}
	\begin{scope}[shift={(0,-6.2)}]\DrawAllInsnOpsBranch\end{scope}
	\begin{scope}[shift={(0,-15.4)}]\DrawAllInsnOpsLoad\end{scope}
	\begin{scope}[shift={(0,-23.1)}]\DrawAllInsnOpsStore\end{scope}
	\begin{scope}[shift={(0,-27.8)}]\DrawAllInsnOpsALUImm\end{scope}
	\begin{scope}[shift={(0,-37.0)}]\DrawAllInsnOpsShiftImm\end{scope}
	\begin{scope}[shift={(0,-41.7)}]\DrawAllInsnOpsALUR\end{scope}

% fence moved out of the I spec
%	\begin{scope}[shift={(0,-56.9)}]\DrawAllInsnOpsFence\end{scope}

	\begin{scope}[shift={(0,-57.4)}]\DrawAllInsnOpsSim\end{scope}
	\begin{scope}[shift={(0,-60.6)}]\DrawAllInsnOpsSystem\end{scope}

	\EndTikzPicture
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\InsnBoxFieldWidthArrowVskip{.5}
\newcommand\InsnBoxFieldWidthArrowHskip{.05}

% #1 MSB position
% #2 LSB position
\newcommand\InsnBoxFieldWidthArrow[2]{
	\pgfmathsetmacro\leftpos{int(31-#1)}		% Calculate the left end position
	\pgfmathsetmacro\wid{int(#1-#2+1)}			% calculate the width
	\begin{scope}[shift={(\leftpos,-\InsnBoxFieldWidthArrowVskip)}]	% Move to left end of arrow & below origin
    	\pgfmathsetmacro\result{\wid*.5+.5}		% the center position
    	\node at (\result,0) {\tiny\wid};		% draw the size number below the box

		\ifthenelse{\wid > 9}					% make 1-9 narrower than 10-99
		{ \pgfmathsetmacro\Inset{0.4} }
		{ 
			\ifthenelse{\wid > 1} 				% make 1 narrower than 2-9
			{ \pgfmathsetmacro\Inset{0.25} }
			{ \pgfmathsetmacro\Inset{0.15} }
		}

		% arrowsInsnBoxFieldWidthArrowHskip
    	\draw[->] (\result+\Inset,0) -- (\wid+.5-\InsnBoxFieldWidthArrowHskip,0);	% arrow to the right
    	\draw[->] (\result-\Inset,0) -- (.5+\InsnBoxFieldWidthArrowHskip,0);		% arrow to the left

		\pgfmathsetmacro\x{.5}
		\pgfmathsetmacro\y{\InsnBoxFieldWidthArrowVskip}
		% vertical bars at the ends of the arrows
    	\draw[-] (\x,\y) -- (\x,-\y*.5);	
		\pgfmathsetmacro\x{(\wid+.5}
    	\draw[-] (\x,\y) -- (\x,-\y*.5);	

	\end{scope}
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\newcommand\BitBoxArrowTailInset{-.9}
\newcommand\BitBoxArrowHeadInset{-16.7-\BitBoxArrowTailInset}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawInsnOpJTypeDecoding{
	\BeginTikzPicture

	\begin{scope}[shift={(0,-1.5)}]

	\DrawInsnTypeJ{abcdefghijklmnopqrst001111101111}

	\pgfmathsetmacro\ArrowNorth{\BitBoxArrowTailInset}
	\pgfmathsetmacro\ArrowSouth{\BitBoxArrowHeadInset}

	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](1,\ArrowSouth);	% 20
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](2,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](3,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](4,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](5,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](6,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](7,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](8,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](9,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](10,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](11,\ArrowSouth);	% sign extend

	\draw[blue,->](1,\ArrowNorth)to[out=270,in=90](12,\ArrowSouth);		% 20
	\draw[blue,->](13,\ArrowNorth)to[out=270,in=90](13,\ArrowSouth);	% 19
	\draw[blue,->](14,\ArrowNorth)to[out=270,in=90](14,\ArrowSouth);	% 18
	\draw[blue,->](15,\ArrowNorth)to[out=270,in=90](15,\ArrowSouth);	% 17
	\draw[blue,->](16,\ArrowNorth)to[out=270,in=90](16,\ArrowSouth);	% 16
	\draw[blue,->](17,\ArrowNorth)to[out=270,in=90](17,\ArrowSouth);	% 15
	\draw[blue,->](18,\ArrowNorth)to[out=270,in=90](18,\ArrowSouth);	% 14
	\draw[blue,->](19,\ArrowNorth)to[out=270,in=90](19,\ArrowSouth);	% 13
	\draw[blue,->](20,\ArrowNorth)to[out=270,in=90](20,\ArrowSouth);	% 12

	\draw[blue,->](12,\ArrowNorth)to[out=270,in=90](21,\ArrowSouth);	% 11

	\draw[blue,->](2,\ArrowNorth)to[out=270,in=90](22,\ArrowSouth);		% 10
	\draw[blue,->](3,\ArrowNorth)to[out=270,in=90](23,\ArrowSouth);		% 9
	\draw[blue,->](4,\ArrowNorth)to[out=270,in=90](24,\ArrowSouth);		% 8
	\draw[blue,->](5,\ArrowNorth)to[out=270,in=90](25,\ArrowSouth);		% 7
	\draw[blue,->](6,\ArrowNorth)to[out=270,in=90](26,\ArrowSouth);		% 6
	\draw[blue,->](7,\ArrowNorth)to[out=270,in=90](27,\ArrowSouth);		% 5
	\draw[blue,->](8,\ArrowNorth)to[out=270,in=90](28,\ArrowSouth);		% 4
	\draw[blue,->](9,\ArrowNorth)to[out=270,in=90](29,\ArrowSouth);		% 3
	\draw[blue,->](10,\ArrowNorth)to[out=270,in=90](30,\ArrowSouth);	% 2
	\draw[blue,->](11,\ArrowNorth)to[out=270,in=90](31,\ArrowSouth);	% 1

	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](32,\ArrowSouth);	% 0 (special case)
	\node at (34.5,\ArrowSouth+5) {0};
%	\draw[blue,->](32,\ArrowSouth+1)to[out=270,in=90](32,\ArrowSouth);	% 0 (special case)
%	\node at (32,\ArrowSouth+2) {0};

	\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}
	\end{scope}

	\begin{scope}[shift={(0,-19.75)}]
		\begin{scope}[shift={(0,1.5)}]
			\DrawInsnBoxCastle{31}{21}
			\DrawInsnBoxCastle{20}{20}
			\DrawInsnBoxCastle{19}{12}
			\DrawInsnBoxCastle{11}{11}
			\DrawInsnBoxCastle{10}{1}
			\DrawInsnBoxCastle{0}{0}
		\end{scope}
		\DrawInsnBoxRel{31}{0}{}
		\draw(33,.5) node[text width = 10, text height = 1, right]{imm\_j};

		\begin{scope}[shift={(0,0)}]\DrawBitstringX{aaaaaaaaaaaamnopqrstlbcdefghijk0}\end{scope}
		\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}

		\InsnBoxFieldWidthArrow{31}{21}
		\InsnBoxFieldWidthArrow{20}{20}
		\InsnBoxFieldWidthArrow{19}{12}
		\InsnBoxFieldWidthArrow{11}{11}
		\InsnBoxFieldWidthArrow{10}{1}
		\InsnBoxFieldWidthArrow{0}{0}
	\end{scope}

	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawInsnOpBTypeDecoding{
	\BeginTikzPicture

	\begin{scope}[shift={(0,-1.5)}]

	\DrawInsnTypeB{abcdefg0111100011000uvwxy1100011}

	\pgfmathsetmacro\ArrowNorth{\BitBoxArrowTailInset}
	\pgfmathsetmacro\ArrowSouth{\BitBoxArrowHeadInset}

	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](1,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](2,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](3,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](4,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](5,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](6,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](7,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](8,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](9,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](10,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](11,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](12,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](13,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](14,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](15,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](16,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](17,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](18,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](19,\ArrowSouth);	% sign extend

	\draw[blue,->](1,\ArrowNorth)to[out=270,in=90](20,\ArrowSouth);		% 12

	\draw[blue,->](25,\ArrowNorth)to[out=270,in=90](21,\ArrowSouth);	% 11

	\draw[blue,->](2,\ArrowNorth)to[out=270,in=90](22,\ArrowSouth);		% 10
	\draw[blue,->](3,\ArrowNorth)to[out=270,in=90](23,\ArrowSouth);		% 9
	\draw[blue,->](4,\ArrowNorth)to[out=270,in=90](24,\ArrowSouth);		% 8
	\draw[blue,->](5,\ArrowNorth)to[out=270,in=90](25,\ArrowSouth);		% 7
	\draw[blue,->](6,\ArrowNorth)to[out=270,in=90](26,\ArrowSouth);		% 6
	\draw[blue,->](7,\ArrowNorth)to[out=270,in=90](27,\ArrowSouth);		% 5

	\draw[blue,->](21,\ArrowNorth)to[out=270,in=90](28,\ArrowSouth);	% 4
	\draw[blue,->](22,\ArrowNorth)to[out=270,in=90](29,\ArrowSouth);	% 3
	\draw[blue,->](23,\ArrowNorth)to[out=270,in=90](30,\ArrowSouth);	% 2
	\draw[blue,->](24,\ArrowNorth)to[out=270,in=90](31,\ArrowSouth);	% 1

	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](32,\ArrowSouth);	% 0 (special case)
	\node at (34.5,\ArrowSouth+5) {0};
%	\draw[blue,->](32,\ArrowSouth+1)to[out=270,in=90](32,\ArrowSouth);	% 0 (special case)
%	\node at (32,\ArrowSouth+2) {0};

	\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}
	\end{scope}

	\begin{scope}[shift={(0,-19.75)}]
		\begin{scope}[shift={(0,1.5)}]
			\DrawInsnBoxCastle{31}{13}
			\DrawInsnBoxCastle{12}{12}
			\DrawInsnBoxCastle{11}{11}
			\DrawInsnBoxCastle{10}{5}
			\DrawInsnBoxCastle{4}{1}
			\DrawInsnBoxCastle{0}{0}
		\end{scope}
		\DrawInsnBoxRel{31}{0}{}
		\draw(33,.5) node[text width = 10, text height = 1, right]{imm\_b};

		\begin{scope}[shift={(0,0)}]\DrawBitstringX{aaaaaaaaaaaaaaaaaaaaybcdefguvwx0}\end{scope}

		\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}

		\InsnBoxFieldWidthArrow{31}{13}
		\InsnBoxFieldWidthArrow{12}{12}
		\InsnBoxFieldWidthArrow{11}{11}
		\InsnBoxFieldWidthArrow{10}{5}
		\InsnBoxFieldWidthArrow{4}{1}
		\InsnBoxFieldWidthArrow{0}{0}
	\end{scope}

	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawInsnOpSTypeDecoding{
	\BeginTikzPicture

	\begin{scope}[shift={(0,-1.5)}]

	\DrawInsnTypeS{abcdefg0111100011000uvwxy0100011}

	\pgfmathsetmacro\ArrowNorth{\BitBoxArrowTailInset}
	\pgfmathsetmacro\ArrowSouth{\BitBoxArrowHeadInset}

	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](1,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](2,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](3,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](4,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](5,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](6,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](7,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](8,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](9,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](10,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](11,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](12,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](13,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](14,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](15,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](16,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](17,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](18,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](19,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](20,\ArrowSouth);	% sign extend

	\draw[blue,->](1,\ArrowNorth)to[out=270,in=90](21,\ArrowSouth);		% 11
	\draw[blue,->](2,\ArrowNorth)to[out=270,in=90](22,\ArrowSouth);		% 10
	\draw[blue,->](3,\ArrowNorth)to[out=270,in=90](23,\ArrowSouth);		% 9
	\draw[blue,->](4,\ArrowNorth)to[out=270,in=90](24,\ArrowSouth);		% 8
	\draw[blue,->](5,\ArrowNorth)to[out=270,in=90](25,\ArrowSouth);		% 7
	\draw[blue,->](6,\ArrowNorth)to[out=270,in=90](26,\ArrowSouth);		% 6
	\draw[blue,->](7,\ArrowNorth)to[out=270,in=90](27,\ArrowSouth);		% 5

	\draw[blue,->](21,\ArrowNorth)to[out=270,in=90](28,\ArrowSouth);	% 4
	\draw[blue,->](22,\ArrowNorth)to[out=270,in=90](29,\ArrowSouth);	% 3
	\draw[blue,->](23,\ArrowNorth)to[out=270,in=90](30,\ArrowSouth);	% 2
	\draw[blue,->](24,\ArrowNorth)to[out=270,in=90](31,\ArrowSouth);	% 1
	\draw[blue,->](25,\ArrowNorth)to[out=270,in=90](32,\ArrowSouth);	% 0

	\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}
	\end{scope}

	\begin{scope}[shift={(0,-19.75)}]
		\begin{scope}[shift={(0,1.5)}]
			\DrawInsnBoxCastle{31}{12}
			\DrawInsnBoxCastle{11}{5}
			\DrawInsnBoxCastle{4}{0}
		\end{scope}
		\DrawInsnBoxRel{31}{0}{}
		\draw(33,.5) node[text width = 10, text height = 1, right]{imm\_s};

		\begin{scope}[shift={(0,0)}]\DrawBitstringX{aaaaaaaaaaaaaaaaaaaaabcdefguvwxy}\end{scope}

		\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}

		\InsnBoxFieldWidthArrow{31}{12}
		\InsnBoxFieldWidthArrow{11}{5}
		\InsnBoxFieldWidthArrow{4}{0}
	\end{scope}

	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawInsnOpITypeDecoding{
	\BeginTikzPicture

	\begin{scope}[shift={(0,-1.5)}]

	\DrawInsnTypeI{abcdefghijkl00011000001110000011}

	\pgfmathsetmacro\ArrowNorth{\BitBoxArrowTailInset}
	\pgfmathsetmacro\ArrowSouth{\BitBoxArrowHeadInset}

	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](1,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](2,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](3,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](4,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](5,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](6,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](7,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](8,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](9,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](10,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](11,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](12,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](13,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](14,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](15,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](16,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](17,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](18,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](19,\ArrowSouth);	% sign extend
	\draw[red,->](1,\ArrowNorth)to[out=270,in=90](20,\ArrowSouth);	% sign extend

	\draw[blue,->](1,\ArrowNorth)to[out=270,in=90](21,\ArrowSouth);		% 11
	\draw[blue,->](2,\ArrowNorth)to[out=270,in=90](22,\ArrowSouth);		% 10
	\draw[blue,->](3,\ArrowNorth)to[out=270,in=90](23,\ArrowSouth);		% 9
	\draw[blue,->](4,\ArrowNorth)to[out=270,in=90](24,\ArrowSouth);		% 8
	\draw[blue,->](5,\ArrowNorth)to[out=270,in=90](25,\ArrowSouth);		% 7
	\draw[blue,->](6,\ArrowNorth)to[out=270,in=90](26,\ArrowSouth);		% 6
	\draw[blue,->](7,\ArrowNorth)to[out=270,in=90](27,\ArrowSouth);		% 5
	\draw[blue,->](8,\ArrowNorth)to[out=270,in=90](28,\ArrowSouth);		% 4
	\draw[blue,->](9,\ArrowNorth)to[out=270,in=90](29,\ArrowSouth);		% 3
	\draw[blue,->](10,\ArrowNorth)to[out=270,in=90](30,\ArrowSouth);	% 2
	\draw[blue,->](11,\ArrowNorth)to[out=270,in=90](31,\ArrowSouth);	% 1
	\draw[blue,->](12,\ArrowNorth)to[out=270,in=90](32,\ArrowSouth);	% 0

	\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}
	\end{scope}

	\begin{scope}[shift={(0,-19.75)}]
		\begin{scope}[shift={(0,1.5)}]
			\DrawInsnBoxCastle{31}{12}
			\DrawInsnBoxCastle{11}{0}
		\end{scope}
		\DrawInsnBoxRel{31}{0}{}
		\draw(33,.5) node[text width = 10, text height = 1, right]{imm\_i};

		\begin{scope}[shift={(0,0)}]\DrawBitstringX{aaaaaaaaaaaaaaaaaaaaabcdefghijkl}\end{scope}

		\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}

		\InsnBoxFieldWidthArrow{31}{12}
		\InsnBoxFieldWidthArrow{11}{0}
	\end{scope}

	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawInsnOpIShiftTypeDecoding{
	\BeginTikzPicture

	\begin{scope}[shift={(0,-1.5)}]

	\DrawInsnTypeI{0100000hijkl00011000001110000011}

	\pgfmathsetmacro\ArrowNorth{\BitBoxArrowTailInset}
	\pgfmathsetmacro\ArrowSouth{\BitBoxArrowHeadInset}
	\pgfmathsetmacro\ZeroNodeY{\ArrowSouth+4}
	\pgfmathsetmacro\ZeroNodeX{1}

	\node at (\ZeroNodeX,\ZeroNodeY) {0};
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=90](1,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=90](2,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=90](3,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](4,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](5,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](6,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](7,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](8,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](9,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](10,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](11,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](12,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](13,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](14,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](15,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](16,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](17,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](18,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](19,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](20,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](21,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](22,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](23,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](24,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](25,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](26,\ArrowSouth);	
	\draw[red,->](\ZeroNodeX+.5,\ZeroNodeY)to[out=0,in=110](27,\ArrowSouth);	

	\draw[blue,->](8,\ArrowNorth)to[out=270,in=90](28,\ArrowSouth);		% 4
	\draw[blue,->](9,\ArrowNorth)to[out=270,in=90](29,\ArrowSouth);		% 3
	\draw[blue,->](10,\ArrowNorth)to[out=270,in=90](30,\ArrowSouth);	% 2
	\draw[blue,->](11,\ArrowNorth)to[out=270,in=90](31,\ArrowSouth);	% 1
	\draw[blue,->](12,\ArrowNorth)to[out=270,in=90](32,\ArrowSouth);	% 0

	\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}
	\end{scope}

	\begin{scope}[shift={(0,-19.75)}]
		\begin{scope}[shift={(0,1.5)}]
			\DrawInsnBoxCastle{31}{5}
			\DrawInsnBoxCastle{4}{0}
		\end{scope}
		\DrawInsnBoxRel{31}{0}{}
		\draw(33,.5) node[text width = 10, text height = 1, right]{shamt\_i};

		\begin{scope}[shift={(0,0)}]\DrawBitstringX{000000000000000000000000000hijkl}\end{scope}

		\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}

		\InsnBoxFieldWidthArrow{31}{5}
		\InsnBoxFieldWidthArrow{4}{0}
	\end{scope}

	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newcommand\DrawInsnOpUTypeDecoding{
	\BeginTikzPicture

	\begin{scope}[shift={(0,-1.5)}]

	\DrawInsnTypeU{abcdefghijklmnopqrst001010110111}

	\pgfmathsetmacro\ArrowNorth{\BitBoxArrowTailInset}
	\pgfmathsetmacro\ArrowSouth{\BitBoxArrowHeadInset}

	\draw[blue,->](1,\ArrowNorth)to[out=270,in=90](1,\ArrowSouth);		% 
	\draw[blue,->](2,\ArrowNorth)to[out=270,in=90](2,\ArrowSouth);		% 
	\draw[blue,->](3,\ArrowNorth)to[out=270,in=90](3,\ArrowSouth);		% 
	\draw[blue,->](4,\ArrowNorth)to[out=270,in=90](4,\ArrowSouth);		% 
	\draw[blue,->](5,\ArrowNorth)to[out=270,in=90](5,\ArrowSouth);		% 
	\draw[blue,->](6,\ArrowNorth)to[out=270,in=90](6,\ArrowSouth);		% 
	\draw[blue,->](7,\ArrowNorth)to[out=270,in=90](7,\ArrowSouth);		% 
	\draw[blue,->](8,\ArrowNorth)to[out=270,in=90](8,\ArrowSouth);		% 
	\draw[blue,->](9,\ArrowNorth)to[out=270,in=90](9,\ArrowSouth);		% 
	\draw[blue,->](10,\ArrowNorth)to[out=270,in=90](10,\ArrowSouth);	% 
	\draw[blue,->](11,\ArrowNorth)to[out=270,in=90](11,\ArrowSouth);	% 
	\draw[blue,->](12,\ArrowNorth)to[out=270,in=90](12,\ArrowSouth);	% 
	\draw[blue,->](13,\ArrowNorth)to[out=270,in=90](13,\ArrowSouth);	% 
	\draw[blue,->](14,\ArrowNorth)to[out=270,in=90](14,\ArrowSouth);	% 
	\draw[blue,->](15,\ArrowNorth)to[out=270,in=90](15,\ArrowSouth);	% 
	\draw[blue,->](16,\ArrowNorth)to[out=270,in=90](16,\ArrowSouth);	% 
	\draw[blue,->](17,\ArrowNorth)to[out=270,in=90](17,\ArrowSouth);	% 
	\draw[blue,->](18,\ArrowNorth)to[out=270,in=90](18,\ArrowSouth);	% 
	\draw[blue,->](19,\ArrowNorth)to[out=270,in=90](19,\ArrowSouth);	% 
	\draw[blue,->](20,\ArrowNorth)to[out=270,in=90](20,\ArrowSouth);	% 

	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](21,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](22,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](23,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](24,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](25,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](26,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](27,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](28,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](29,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](30,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](31,\ArrowSouth);	% 
	\draw[red,->](34,\ArrowSouth+5)to[out=180,in=90](32,\ArrowSouth);	% 0 (special case)
	\node at (34.5,\ArrowSouth+5) {0};

	\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}
	\end{scope}

	\begin{scope}[shift={(0,-19.75)}]
		\begin{scope}[shift={(0,1.5)}]
			\DrawInsnBoxCastle{31}{12}
			\DrawInsnBoxCastle{11}{0}
		\end{scope}
		\DrawInsnBoxRel{31}{0}{}
		\draw(33,.5) node[text width = 10, text height = 1, right]{imm\_u};

		\begin{scope}[shift={(0,0)}]\DrawBitstringX{abcdefghijklmnopqrst000000000000}\end{scope}

		\begin{scope}[shift={(0,0)}]\DrawHexMarkersRel{32}\end{scope}

		\InsnBoxFieldWidthArrow{31}{12}
		\InsnBoxFieldWidthArrow{11}{0}
	\end{scope}

	\EndTikzPicture
}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% print a register name in typewriter font
\newcommand\reg[1]{{\tt #1}}

% print an instruction name in typewriter font
\newcommand\insn[1]{{\tt #1}}

\newcommand\rvddt{{\tt rvddt}}

\newcommand\hex[1]{{\tt 0x#1}}
